package xyz.lne.researchgroupmanage.service.dataTransferObject;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import xyz.lne.researchgroupmanage.entity.SysUser;
import xyz.lne.researchgroupmanage.security.UserAuthority;
import xyz.lne.researchgroupmanage.security.UserPower;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Designed By Lne.
 * 2021/1/22 15:39
 */
@Getter
@AllArgsConstructor
public class JwtUserDTO implements UserDetails {
    private final  Integer userId;
    private final String account;
    @JsonIgnore
    private final String password;

    @JsonIgnore
    private final List<GrantedAuthority> authorities;

    @Override
    public String getUsername() {
        return this.getAccount();
    }
//    password已经使用getter

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }

    public JwtUserDTO(UserDTO userDTO, List<ResearchGroupSimpleDTO> researchGroupManageSimpleDTOList, List<ResearchGroupSimpleDTO> researchGroupJoinSimpleDTOList,
                      List<ResearchSimpleDTO> researchManageSimpleDTOList, List<ResearchSimpleDTO> researchJoinSimpleDTOList,
                      List<TaskSimpleDTO> participatedTaskList){
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.addAll(researchGroupManageSimpleDTOList.stream().map(UserAuthority::researchGroupManage).collect(Collectors.toList()));
        authorities.addAll(researchGroupJoinSimpleDTOList.stream().map(UserAuthority::researchGroupJoin).collect(Collectors.toList()));
        authorities.addAll(researchManageSimpleDTOList.stream().map(UserAuthority::researchManage).collect(Collectors.toList()));
        authorities.addAll(researchJoinSimpleDTOList.stream().map(UserAuthority::researchJoin).collect(Collectors.toList()));
        authorities.addAll(participatedTaskList.stream().map(UserAuthority::participateTask).collect(Collectors.toList()));
        this.userId = userDTO.getUserId();
        this.account = userDTO.getAccount();
        this.password = userDTO.getPassword();
        this.authorities = authorities;
    }
}
